﻿using System;
using System.Collections.Generic;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System.Linq;

namespace YatFram.Browser.Selenium
{
    public class SeleniumBrowser : IBrowser
    {
        readonly IWebDriver driver;// = new InternetExplorerDriver();

        public SeleniumBrowser(string page)
        {
            driver = new InternetExplorerDriver();
            GoTo(page);
        }

        public void Dispose()
        {
            driver.Dispose();
        }

        public void GoTo(string url)
        {
            driver.Navigate().GoToUrl(url);
        }

        public string Dump()
        {
            return driver.PageSource;
        }

        public bool Exists
        {
            get { throw new NotImplementedException(); }
        }

//        public IEnumerable<IContainerElement> Find(string cssSelector)
//        {
//            return new SeleniumContainerElement(() => driver.FindElement(cssSelector));
//        }
        public IEnumerable<IContainerElement> Find(CssSelector.CssSelectors selector)
        {
            return driver.FindElements(SeleniumConstraints.ToSeleniumConstraint(selector)).Select(e => (IContainerElement)new SeleniumContainerElement(() => e));
        }

        public InputElement FindTextBox(string cssSelector)
        {
            return new SeleniumTextBox(driver.FindElement(cssSelector));
        }

        public ISingleOption FindSelect(string cssSelector)
        {
            return new SeleniumRadioBox(driver.FindElement(cssSelector).FindElements(By.TagName("option")));
        }

        public ISingleOption FindRadio(string name)
        {
            return new SeleniumRadioBox(driver.FindElements(By.Name(name)));
        }

        public IMultipleOptions FindCheckbox(string name)
        {
            return new SeleniumMultipleOptions(driver.FindElements(By.Name(name)));
        }
    }
}